<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue-swiper</title>
    <link rel="stylesheet" href="lib/swiper.css">
    <script src="lib/vue.js"></script>
    <script src="lib/axios.min.js"></script>
    <script src="lib/swiper.js"></script>
    <style>
        img {
            width: 100%;
            height: 300px;
        }
    </style>
</head>

<body>
    <div id="box">
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <div class="swiper-slide" v-for="data in datalist" :key="data.channelid">
                    <img :src="data.thumb">
                </div>
            </div>
            <!-- 如果需要分页器 -->
            <div class="swiper-pagination"></div>

            <!-- 如果需要导航按钮 -->
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>
        </div>

    </div>

    <script>
        new Vue({
            el: "#box",
            data: {
                datalist: []
            },
            mounted() {
                axios.get("https://api.apiopen.top/musicBroadcasting").then(res => {
                    console.log(res.data.result[0].channellist);
                    this.datalist = res.data.result[0].channellist;
                    console.log("mounted");
                    //但是因为是异步请求，导致swiper初始化过早，所以，解决方案：
                    //swiper
                    // var mySwiper = new Swiper('.swiper-container', {
                    //     direction: 'vertical', // 垂直切换选项
                    //     loop: true, // 循环模式选项

                    //     // 如果需要分页器
                    //     pagination: {
                    //         el: '.swiper-pagination',
                    //     },

                    //     // 如果需要前进后退按钮
                    //     navigation: {
                    //         nextEl: '.swiper-button-next',
                    //         prevEl: '.swiper-button-prev',
                    //     },
                    // })
                }).catch(err => {
                    console.log(err);
                })
            },
            //解决方案1:
            updated() {
                console.log("swiper");
                //swiper
                new Swiper('.swiper-container', {
                    direction: 'horizontal', // 垂直切换选项
                    loop: true, // 循环模式选项

                    // 如果需要分页器
                    pagination: {
                        el: '.swiper-pagination',
                    },

                    // 如果需要前进后退按钮
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                })

            }
        })
    </script>

</body>

</html>